<!DOCTYPE html>
<html lang="zh-cmn-Hans" prefix="og: http://ogp.me/ns#" class="han-init">
<head>
  <meta charset="utf-8">
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <link rel="icon" href="http://upeng.github.io/favicon.ico">
  <title>upeng</title>
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/lib/fancybox/jquery.fancybox-1.3.4.css">
  <!--在这里倒入jquery 方便处理部分页面的jquery-->
  <script src="https://cdn.staticfile.org/jquery/1.7/jquery.min.js" type="text/javascript" ></script>
</head>

<body>
	<header class="site-header navfixed-false">
  <div class="container">
      <h1><a href="/" title="upeng"><span class="octicon octicon-mark-github"></span> upeng</a></h1>
      <nav class="site-header-nav" role="navigation">
        
              
              <a href="/"  class=" site-header-nav-item hvr-underline-from-center" title="Home">Home</a>
        
              
              <a href="/categories/"  class=" site-header-nav-item hvr-underline-from-center" title="Categories">Categories</a>
        
              
              <a href="/bookmark/"  class=" site-header-nav-item hvr-underline-from-center" title="Bookmark">Bookmark</a>
        
              
              <a href="http://shareup.sinaapp.com"  class=" site-header-nav-item hvr-underline-from-center" title="Share">Share</a>
        
              
              <a href="/about/"  class=" site-header-nav-item hvr-underline-from-center" title="About">About</a>
        
      </nav>
  </div>
</header>

	
<section class="collection-head geopattern" data-pattern-id="Mac下安装LNMP(Nginx+PHP5.6)环境" >
    <div class="container">
        <div class="collection-title">
            <h1 class="collection-header">
                Mac下安装LNMP(Nginx+PHP5.6)环境
            </h1>
            
                <div class="collection-info">
                    <span class="meta-info">
                        <span class="octicon octicon-calendar"></span>
                        <time datetime="2016-08-28T14:28:49.000Z" itemprop="datePublished">2016-08-28</time>
                    </span>
                    
                        <span class="meta-info">
                            <span class="octicon octicon-file-directory"></span>
                            <a href='/categories/乱炖/' title=''>乱炖</a>
                        </span>
                    
                </div>
            
        </div>
    </div>
</section>
	<section class="container">
    <div class="columns">
        <!-- -->
        <div class="column three-fourths">
            <article class="article-content markdown-body">
                <h2 id="安装Homebrew"><a href="#安装Homebrew" class="headerlink" title="安装Homebrew"></a>安装Homebrew</h2><p>从零开始安装Mac下LNMP环境的过程</p>
<blockquote>
<p>确保系统已经安装xcode，然后使用一行命令安装依赖管理工具Homebrew</p>
</blockquote>
<p>安装完xcode之后可以使用</p>
<pre><code>brew install FORMULA
</code></pre><p>来安装所需要的依赖了.更详细的信息参考Homebrew的[官方Cookbook][1]</p>
<p>因此使用Homebrew常见的流程是：<br><a id="more"></a></p>
<ol>
<li>增加一个程序源（新增一个水龙头） brew tap homebrew/php</li>
<li>更新程序源 brew update</li>
<li>安装程序包（按照配方酿酒） brew install git</li>
<li>查看配置 brew config 可以看到程序包默认安装在/usr/local/Cellar下 （酒桶放在地窖内）</li>
</ol>
<h2 id="安装PHP5-6（FPM方式）"><a href="#安装PHP5-6（FPM方式）" class="headerlink" title="安装PHP5.6（FPM方式）"></a>安装PHP5.6（FPM方式）</h2><p>首先加入Homebrew官方的几个软件源</p>
<pre><code>brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/php
</code></pre><p>PHP如果采用默认配置安装，会编译mod_php模块并只运行在Apache环境下，为了使用Nginx，这里需要编译php-fpm并且禁用apache，主要通过参数<code>--without-fpm --without-apache</code>来实现。完整的安装指令为</p>
<pre><code>brew install php56 \
--without-snmp \
--without-apache \
--with-debug \
--with-fpm \
--with-intl \
--with-homebrew-curl \
--with-homebrew-libxslt \
--with-homebrew-openssl \
--with-imap \
--with-mysql \
--with-tidy
</code></pre><p>由于OSX已经自带了PHP5.5环境，因此需要修改系统路径，优先运行brew安装的版本，在<code>~/.bashrc</code>里加入：</p>
<pre><code>export PATH=&quot;/usr/local/bin:/usr/local/sbin:$PATH&quot;
</code></pre><blockquote>
<p>这时运行 php -v查看版本是否是5.6<br>运行php -m 查看PHP安装都扩展列表</p>
</blockquote>
<p>如果要安装新的php扩展，可以直接安装而不用每次重新编译php，所有的扩展可以通过</p>
<pre><code>brew search php56
</code></pre><p>看到，下面是我自己所需要的扩展，可以支持Phalcon框架：</p>
<pre><code>brew install php56-gearman php56-msgpack php56-memcache php56-memcached php56-mongo  php56-phalcon php56-redis php56-xdebug
</code></pre><h3 id="PHP-FPM的加载与启动"><a href="#PHP-FPM的加载与启动" class="headerlink" title="PHP-FPM的加载与启动"></a>PHP-FPM的加载与启动</h3><p>php-fpm.conf  php.ini</p>
<pre><code>/usr/local/etc/php/5.6
</code></pre><p>php-fpm进程</p>
<pre><code>/usr/local/sbin
</code></pre><p><strong>安装完毕后可以通过以下指令启动和停止php-fpm</strong></p>
<pre><code>./php-fpm -D    //进入sbin目录

killall php-fpm
</code></pre><p>同时可以将php-fpm加入开机启动</p>
<pre><code>ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
</code></pre><h3 id="查看php-fpm进程"><a href="#查看php-fpm进程" class="headerlink" title="查看php-fpm进程"></a>查看php-fpm进程</h3><p>查看9000端口<br><code>lsof -i:9000</code><br>或者<br><code>ps -ef | grep php-fpm</code></p>
<h2 id="安装Nginx"><a href="#安装Nginx" class="headerlink" title="安装Nginx"></a>安装Nginx</h2><pre><code>brew install nginx
</code></pre><p>安装完毕后可以通过</p>
<pre><code>nginx
nginx -s quit
</code></pre><p>启动和关闭，同时也支持重载配置文件等操作</p>
<pre><code>nginx -s reload|reopen|stop|quit
</code></pre><p>nginx安装后默认监听8080端口，可以访问<a href="http://localhost:8080" target="_blank" rel="external">http://localhost:8080</a> 查看状态。如果要想监听80端口需要root权限，运行</p>
<pre><code>sudo chown root:wheel /usr/local/Cellar/nginx/1.6.2/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.6.2/bin/nginx
</code></pre><blockquote>
<p>我是直接在nginx配置中改成80端口，然后重启生效<br>并使用root权限启动</p>
</blockquote>
<pre><code>sudo nginx
</code></pre><p>开机启动</p>
<pre><code>ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
</code></pre><h2 id="Nginx-PHP-FPM配置"><a href="#Nginx-PHP-FPM配置" class="headerlink" title="Nginx + PHP-FPM配置"></a>Nginx + PHP-FPM配置</h2><p>好文：<a href="http://www.nginx.cn/nginx-how-to" target="_blank" rel="external">http://www.nginx.cn/nginx-how-to</a></p>
<p>Nginx一般都会运行多个域名（虚拟主机）</p>
<blockquote>
<p>按Ubuntu的文件夹结构来存放Nginx的配置文件</p>
</blockquote>
<pre><code>mkdir -p /usr/local/var/logs/nginx
mkdir -p /usr/local/etc/nginx/sites-available
mkdir -p /usr/local/etc/nginx/sites-enabled
mkdir -p /usr/local/etc/nginx/conf.d
mkdir -p /usr/local/etc/nginx/ssl
</code></pre><blockquote>
<p>联想到work中，vhost和fpm.d目录，新增域名与目录时，只需要增加两个配置文件；前提时php-fpm.conf和nginx.conf主配置文件中都有</p>
</blockquote>
<pre><code>include vhost/*.conf
include fpm.d/*.conf
</code></pre><p><strong>上述都conf.d等同于fpm.d，sites-enabled等同于vhost</strong></p>
<h3 id="编辑Nginx全局配置"><a href="#编辑Nginx全局配置" class="headerlink" title="编辑Nginx全局配置"></a>编辑Nginx全局配置</h3><pre><code>vim /usr/local/etc/nginx/nginx.conf
</code></pre><pre><code>worker_processes  1;
error_log   /usr/local/var/logs/nginx/error.log debug;
pid        /usr/local/var/run/nginx.pid;

events {
    worker_connections  256;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format main &#39;$remote_addr - $remote_user [$time_local] &#39;
        &#39;&quot;$request&quot; $status $body_bytes_sent &#39;
        &#39;&quot;$http_referer&quot; &quot;$http_user_agent&quot; &#39;
        &#39;&quot;$http_x_forwarded_for&quot; $host $request_time $upstream_response_time $scheme &#39;
        &#39;$cookie_evalogin&#39;;

    access_log  /usr/local/var/logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/sites-enabled/*;
}
</code></pre><p>这样一来首先可以把一些可复用配置独立出来放在<code>/usr/local/etc/nginx/conf.d</code>下，比如fastcgi的设置就可以独立出来</p>
<pre><code>vim /usr/local/etc/nginx/conf.d/php-fpm
</code></pre><p>内容为</p>
<pre><code>location ~ \.php$ {
    try_files                   $uri = 404;
    fastcgi_pass                127.0.0.1:9000;
    fastcgi_index               index.php;
    fastcgi_intercept_errors    on;
    include /usr/local/etc/nginx/fastcgi.conf;
}
</code></pre><p>然后<code>/usr/local/etc/nginx/sites-enabled</code>目录下可以一个文件对应一个域名的配置，比如web服务器目录是<code>/opt/htdocs</code></p>
<pre><code>vim /usr/local/etc/nginx/sites-enabled/default
</code></pre><pre><code>server {
    listen       80;
    server_name  localhost;
    root         /opt/htdocs/;

    location / {
        index  index.html index.htm index.php;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }
}
</code></pre><p>此时启动了php-fpm并且启动了Nginx后，就可以通过<a href="http://localhost来运行php程序了" target="_blank" rel="external">http://localhost来运行php程序了</a></p>
<h2 id="安装MySQL"><a href="#安装MySQL" class="headerlink" title="安装MySQL"></a>安装MySQL</h2><pre><code>brew install mysql
</code></pre><p>可以通过</p>
<pre><code>mysql.server start
mysql.server stop
</code></pre><p>来启动／停止，启动后默认应为空密码，可以通过mysqladmin设置一个密码</p>
<pre><code>mysqladmin -uroot password &quot;mypassword&quot;
</code></pre><p>但是在操作的时候出现了空密码无法登入的情况，最终只能通过mysqld_safe来设置</p>
<pre><code>sudo mysqld_safe --skip-grant-tables
mysql -u root
mysql&gt; UPDATE mysql.user SET Password=PASSWORD(&#39;mypassword&#39;) WHERE User=&#39;root&#39;;
mysql&gt; FLUSH PRIVILEGES;
</code></pre><p><strong>依旧报错</strong></p>
<blockquote>
<p><strong>坑1</strong>：一直出现mysql.sock错误，<code>mysql -uroot -p</code>;<br><strong>填坑1</strong>：<code>mysql -h 127.0.0.1 -u root -p</code>  why?</p>
<p><strong>坑2</strong>：设置密码报错：<code>Password</code>字段不存在<br><strong>填坑2</strong>：mysql5.7已将<code>Password</code>字段去掉，改成<code>authentication_string</code>；所以正确都姿势如下：</p>
<pre><code>update mysql.user set authentication_string=password(&#39;123qwe&#39;) where user=&#39;root
</code></pre><p>老版本mysql没变</p>
</blockquote>
<p>最后将MySQL加入开机启动</p>
<pre><code>cp /usr/local/Cellar/mysql/5.6.22/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
</code></pre><h2 id="查看服务列表"><a href="#查看服务列表" class="headerlink" title="查看服务列表"></a>查看服务列表</h2><pre><code>brew services list
</code></pre><pre><code>Name  Status  User    Plist
mysql started root    /Library/LaunchDaemons/homebrew.mxcl.mysql.plist
nginx started yupeng6 /Users/yupeng6/Library/LaunchAgents/homebrew.mxcl.nginx.plist
php56 started yupeng6 /Users/yupeng6/Library/LaunchAgents/homebrew.mxcl.php56.plist
</code></pre><p>安装redis和memcache过程省略，详见下面博文，本文也是参考此<br><a href="http://avnpc.com/pages/install-lnmp-on-osx" target="_blank" rel="external">http://avnpc.com/pages/install-lnmp-on-osx</a></p>

            </article>
            
                <div class="share">
                    <!--开启分享-->
<div class="share-component" data-disabled="google,twitter,facebook" data-description="安装Homebrew从零开始安装Mac下LNMP环境的..."></div>

<script src="/js/share.min.js"></script>

                </div>    
            
            
                
<div class="comments">
	<div class="ds-thread" data-thread-key="mac-lnmp" data-title="Mac下安装LNMP(Nginx+PHP5.6)环境" data-url="http://upeng.github.io/2016/08/28/mac-lnmp/"></div>
	<script type="text/javascript">
	var duoshuoQuery = {short_name:"upeng"};
		(function() {
			var ds = document.createElement('script');
			ds.type = 'text/javascript';ds.async = true;
			ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
			ds.charset = 'UTF-8';
			(document.getElementsByTagName('head')[0]
			 || document.getElementsByTagName('body')[0]).appendChild(ds);
		})();
	</script>
</div>

            
        </div>
        <div class="column one-fourth">
            
                
                


<h3>Post Directory</h3>

<div id="post-directory-module">
	<section class="post-directory">
		<p><strong class="toc-title">文章目录</strong></p>
		<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#安装Homebrew"><span class="toc-text">安装Homebrew</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#安装PHP5-6（FPM方式）"><span class="toc-text">安装PHP5.6（FPM方式）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#PHP-FPM的加载与启动"><span class="toc-text">PHP-FPM的加载与启动</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#查看php-fpm进程"><span class="toc-text">查看php-fpm进程</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#安装Nginx"><span class="toc-text">安装Nginx</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Nginx-PHP-FPM配置"><span class="toc-text">Nginx + PHP-FPM配置</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#编辑Nginx全局配置"><span class="toc-text">编辑Nginx全局配置</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#安装MySQL"><span class="toc-text">安装MySQL</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#查看服务列表"><span class="toc-text">查看服务列表</span></a></li></ol>
	</section>
</div>
            
        </div>
    </div>
</section>

<footer class="container">
    <div class="site-footer" role="contentinfo">
        <div class="copyright left mobile-block">
                © 2016
                <span title="yupeng">yupeng</span>
                <a href="javascript:window.scrollTo(0,0)" class="right mobile-visible">TOP</a>
        </div>

        <ul class="site-footer-links right mobile-hidden">
            <li>
                <a href="javascript:window.scrollTo(0,0)" >TOP</a>
            </li>
        </ul>

        <a href="https://github.com/upeng" target="_blank" aria-label="view source code">
            <span class="mega-octicon octicon-mark-github" title="GitHub"></span>
        </a>

        <ul class="site-footer-links mobile-hidden">
            
                  
                  <li>
                    <a href="/"  title="Home">Home</a>
                  </li>
            
                  
                  <li>
                    <a href="/categories/"  title="Categories">Categories</a>
                  </li>
            
                  
                  <li>
                    <a href="/bookmark/"  title="Bookmark">Bookmark</a>
                  </li>
            
                  
                  <li>
                    <a href="http://shareup.sinaapp.com"  title="Share">Share</a>
                  </li>
            
                  
                  <li>
                    <a href="/about/"  title="About">About</a>
                  </li>
            
            <li>
                <a href="/atom.xml">
                    <span class="octicon octicon-rss" style="color:orange;"></span>
                </a>
            </li>
        </ul>
    </div>
</footer>


		<script src="/js/geopattern.js"></script>

		
			<script src="/js/toc.js"></script>
		

		<script src="/js/highlight.pack.js"></script>
		<script src="/lib/fancybox/jquery.fancybox-1.3.4.pack.js"></script>

		<script src="/js/index.js"></script>

		 <script src="/js/popular_repo.js"></script> 

	</body>
</html>